var GameObjects = JW.Observable.extend({
    // Events:
    // gameend

    FieldObjects: null,
    GreenMonsterObjects: null,
    init: function () {
        this.FieldObjects = [];
        this.GreenMonsterObjects = [];
    },
    AddFieldObject: function (GameElement) {
        this.FieldObjects.push(GameElement);
    },
    AddGreenMonsterObject: function (GameElement) {
        this.GreenMonsterObjects.push(GameElement);
    },
    DeleteObject: function (i) {
        this.FieldObjects[i].destroy();
        this.FieldObjects[i] = this.FieldObjects[this.FieldObjects.length - 1];
        this.FieldObjects.pop();
        return true;
    },
    touchCheck: function (GreenMonster, i) {
        if ((this.FieldObjects[i].top < GreenMonster.top) && (this.FieldObjects[i].top + this.FieldObjects[i].height > GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].left > GreenMonster.left) && (this.FieldObjects[i].left + this.FieldObjects[i].width < GreenMonster.left + GreenMonster.width)) return true;
        if ((this.FieldObjects[i].top > GreenMonster.top) && (this.FieldObjects[i].top + this.FieldObjects[i].height < GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].left < GreenMonster.left) && (this.FieldObjects[i].left + this.FieldObjects[i].width > GreenMonster.left + GreenMonster.width)) return true;
        if ((this.FieldObjects[i].top < GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].top > GreenMonster.top) && (this.FieldObjects[i].left < GreenMonster.left + GreenMonster.width) && (this.FieldObjects[i].left > GreenMonster.left)) return true;
        if ((this.FieldObjects[i].top < GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].top > GreenMonster.top) && (this.FieldObjects[i].left + this.FieldObjects[i].width < GreenMonster.left + GreenMonster.width) && (this.FieldObjects[i].left + this.FieldObjects[i].width > GreenMonster.left)) return true;
        if ((this.FieldObjects[i].top + this.FieldObjects[i].height < GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].top + this.FieldObjects[i].height > GreenMonster.top) && (this.FieldObjects[i].left < GreenMonster.left + GreenMonster.width) && (this.FieldObjects[i].left > GreenMonster.left)) return true;
        if ((this.FieldObjects[i].top + this.FieldObjects[i].height < GreenMonster.top + GreenMonster.height) && (this.FieldObjects[i].top + this.FieldObjects[i].height > GreenMonster.top) && (this.FieldObjects[i].left + this.FieldObjects[i].width < GreenMonster.left + GreenMonster.width) && (this.FieldObjects[i].left + this.FieldObjects[i].width > GreenMonster.left)) return true;
        if ((GreenMonster.top < this.FieldObjects[i].top + this.FieldObjects[i].height) && (GreenMonster.top > this.FieldObjects[i].top) && (GreenMonster.left < this.FieldObjects[i].left + this.FieldObjects[i].width) && (GreenMonster.left > this.FieldObjects[i].left)) return true;
        if ((GreenMonster.top < this.FieldObjects[i].top + this.FieldObjects[i].height) && (GreenMonster.top > this.FieldObjects[i].top) && (GreenMonster.left + GreenMonster.width < this.FieldObjects[i].left + this.FieldObjects[i].width) && (GreenMonster.left + GreenMonster.width > this.FieldObjects[i].left)) return true;
        if ((GreenMonster.top + GreenMonster.height < this.FieldObjects[i].top + this.FieldObjects[i].height) && (GreenMonster.top + GreenMonster.height > this.FieldObjects[i].top) && (GreenMonster.left < this.FieldObjects[i].left + this.FieldObjects[i].width) && (GreenMonster.left > this.FieldObjects[i].left)) return true;
        if ((GreenMonster.top + GreenMonster.height < this.FieldObjects[i].top + this.FieldObjects[i].height) && (GreenMonster.top + GreenMonster.height > this.FieldObjects[i].top) && (GreenMonster.left + GreenMonster.width < this.FieldObjects[i].left + this.FieldObjects[i].width) && (GreenMonster.left + GreenMonster.width > this.FieldObjects[i].left)) return true;
        return false;
    },
    updateFieldObjects: function (GreenMonster, ScoreBar, LifeBar) {
        var endcheck = false;
        for (var i = 0; i < this.FieldObjects.length; ++i) {
            endcheck = false;
            this.FieldObjects[i].time += 1;
            if (this.FieldObjects[i].hp < 1) {
                if (this.FieldObjects[i].type == "appleEnemy") ScoreBar.status += 5;
                if (this.FieldObjects[i].type == "vineEnemy") ScoreBar.status += 15;
                if (this.FieldObjects[i].type == "CocoEnemy") ScoreBar.status += 100;
                if (this.FieldObjects[i].type == "BananaBunchEnemy") {
                    ScoreBar.status += 10;

                    var Banana = new BananaEnemy({ width: 90, height: 37, speedL: 8, type: "BananaEnemy", hp: 10,
                        left: Math.min(710, (this.FieldObjects[i].left + 90 - 32)), top: this.FieldObjects[i].top
                    });
                    this.AddFieldObject(Banana);

                    Banana = new BananaEnemy({ width: 90, height: 37, speedL: 8, type: "BananaEnemy", hp: 10,
                        left: Math.min(710, (this.FieldObjects[i].left + 90 - 32)),
                        top: Math.max(0, (this.FieldObjects[i].top - 100))
                    });
                    this.AddFieldObject(Banana);

                    Banana = new BananaEnemy({ width: 90, height: 37, speedL: 8, type: "BananaEnemy", hp: 10,
                        left: Math.min(710, (this.FieldObjects[i].left + 90 - 32)),
                        top: Math.min(563, (this.FieldObjects[i].top + 100))
                    });
                    this.AddFieldObject(Banana);
                }
                if (this.FieldObjects[i].type == "BananaEnemy") ScoreBar.status += 30;
                if (this.FieldObjects[i].type == "EvilTree") {
                    this.pauseWindow = $("<div class='pauseWindow' />")
                    $(".GameField").append(this.pauseWindow);
                    var pauseWindowBackToMenu = new Button
						({
						    top: 283,
						    left: 360,
						    width: 80,
						    height: 35,
						    text: "Menu"
						});
                    var pauseWindowBackToMenuView = new MenuButton(pauseWindowBackToMenu, "pauseWindow");
                    $(".Menu").addClass("pauseWindowBackToMenu");
                    clearInterval(NewGameController.Timer);
                    $(".pauseWindowBackToMenu").click((function () {
                        ScoreField.remove();
                        this.pauseWindow.remove();
                        delete this.pauseWindow;
                        NewGameController.destroy();
                        pauseWindowBackToMenuView.destroy();
                        GameField.remove();
                        $(".MenuButton").css({ visibility: "visible" });
                        $(".MenuWindow").css({ visibility: "visible" });
                    }).inScope(this));
                    this.trigger("Lvl1Clear");
                }
                var bonusNum = Math.floor(Math.random() * this.FieldObjects[i].bonus.length);
                if (this.FieldObjects[i].bonus[bonusNum] == "Apple") {
                    this.Apple = new Apple({
                        top: Math.min(this.FieldObjects[i].top, 550),
                        left: Math.min(this.FieldObjects[i].left, 750),
                        width: 50,
                        height: 50,
                        type: "Apple"
                    });
                    this.FieldObjects[i].destroy();
                    this.FieldObjects[i] = this.Apple;
                }
                else
                    if (this.FieldObjects[i].bonus[bonusNum] == "DoubleBonus") {
                        this.Bonus = new DoubleBonus({
                            top: Math.min(this.FieldObjects[i].top, 539),
                            left: Math.min(this.FieldObjects[i].left, 740),
                            width: 60,
                            height: 61,
                            type: "DoubleBonus"
                        });
                        this.FieldObjects[i].destroy();
                        this.FieldObjects[i] = this.Bonus;
                    }
                    else
                        if (this.FieldObjects[i].bonus[bonusNum] == "tripleBonus") {
                            this.Bonus = new TripleBonus({
                                top: Math.min(this.FieldObjects[i].top, 539),
                                left: Math.min(this.FieldObjects[i].left, 740),
                                width: 60,
                                height: 61,
                                type: "tripleBonus"
                            });
                            this.FieldObjects[i].destroy();
                            this.FieldObjects[i] = this.Bonus;
                        }
                        else
                            if (this.FieldObjects[i].bonus[bonusNum] == "ShrapnelBonus") {
                                this.Bonus = new ShrapnelBonus({
                                    top: Math.min(this.FieldObjects[i].top, 539),
                                    left: Math.min(this.FieldObjects[i].left, 740),
                                    width: 60,
                                    height: 61,
                                    type: "ShrapnelBonus"
                                });
                                this.FieldObjects[i].destroy();
                                this.FieldObjects[i] = this.Bonus;
                            }
                            else
                                if (this.FieldObjects[i].bonus[bonusNum] == "laserBonus") {
                                    this.Bonus = new LaserBonus({
                                        top: Math.min(this.FieldObjects[i].top, 544),
                                        left: Math.min(this.FieldObjects[i].left, 745),
                                        width: 55,
                                        height: 56,
                                        type: "laserBonus"
                                    });
                                    this.FieldObjects[i].destroy();
                                    this.FieldObjects[i] = this.Bonus;
                                }
                                else {
                                    this.FieldObjects[i].destroy();
                                    this.FieldObjects[i] = this.FieldObjects[this.FieldObjects.length - 1];
                                    this.FieldObjects.pop();
                                }
                endcheck = true;
            }
            else if (this.FieldObjects[i].left < 0 || this.FieldObjects[i].top < 0 || this.FieldObjects[i].top + this.FieldObjects[i].height > 600 || this.FieldObjects[i].left + this.FieldObjects[i].width > 800) endcheck = this.DeleteObject(i);
            if (!endcheck) this.FieldObjects[i].UpdatePosition();
            if (!endcheck)
                if (this.touchCheck(GreenMonster, i)) {
                    if (!endcheck)
                        if (this.FieldObjects[i].type == "Apple") {
                            ScoreBar.status += 15;
                            endcheck = this.DeleteObject(i);
                        }
                        else
                            if (this.FieldObjects[i].type == "ShrapnelBonus") {
                                GreenMonster.weapons[4] += 50;
                                endcheck = this.DeleteObject(i);
                            }
                            else
                                if (this.FieldObjects[i].type == "tripleBonus") {
                                    GreenMonster.weapons[2] += 100;
                                    endcheck = this.DeleteObject(i);
                                }
                                else
                                    if (this.FieldObjects[i].type == "DoubleBonus") {
                                        GreenMonster.weapons[1] += 100;
                                        endcheck = this.DeleteObject(i);
                                    }
                                    else
                                        if (this.FieldObjects[i].type == "laserBonus") {
                                            GreenMonster.weapons[3] += 100;
                                            endcheck = this.DeleteObject(i);
                                        }
                    if (!endcheck)
                        if ((this.FieldObjects[i].type == "appleEnemy" || this.FieldObjects[i].type == "evilAppleBullet" || this.FieldObjects[i].type == "vineEnemyBullet" ||
                    this.FieldObjects[i].type == "vineEnemy" || this.FieldObjects[i].type == "CocoEnemy" || this.FieldObjects[i].type == "CocoEnemyBullet" ||
                    this.FieldObjects[i].type == "BananaEnemy" || this.FieldObjects[i].type == "BananaBunchEnemy"
                    || this.FieldObjects[i].type == "EvilTree") && GreenMonster.invul == 0
                    ) {
                            GreenMonster.lifes -= 1;
                            if (GreenMonster.lifes == 0) {
                                this.pauseWindow = $("<div class='pauseWindow' />")
                                $(".GameField").append(this.pauseWindow);
                                var pauseWindowBackToMenu = new Button
						({
						    top: 283,
						    left: 360,
						    width: 80,
						    height: 35,
						    text: "Menu"
						});
                                var pauseWindowBackToMenuView = new MenuButton(pauseWindowBackToMenu, "pauseWindow");
                                $(".Menu").addClass("pauseWindowBackToMenu");
                                clearInterval(NewGameController.Timer);
                                $(".pauseWindowBackToMenu").click((function () {
                                    ScoreField.remove();
                                    this.pauseWindow.remove();
                                    delete this.pauseWindow;
                                    NewGameController.destroy();
                                    pauseWindowBackToMenuView.destroy();
                                    GameField.remove();
                                    $(".MenuButton").css({ visibility: "visible" });
                                    $(".MenuWindow").css({ visibility: "visible" });
                                }).inScope(this));
                                this.trigger("gameend");
                            }
                            else GreenMonster.invul += 3000;
                            LifeBar.status -= 1;
                            if (this.FieldObjects[i].type != "EvilTree") endcheck = this.DeleteObject(i);
                        }
                }
            if (!endcheck)
            {
                var BulletArray = this.FieldObjects[i].shoot(GreenMonster);
			    for (var count=0; count<BulletArray.length; count++)
                    this.AddFieldObject(BulletArray[count]);
		    }
        }
    },
    updateGreenMonsterObjects: function (GreenMonster) {
        var endcheck = false;
        for (var i = 0; i < this.GreenMonsterObjects.length; ++i)
        {
            this.GreenMonsterObjects[i].UpdatePosition();
            endcheck = false;
            for (var j = 0; j < this.FieldObjects.length && (!endcheck); ++j)
            {
                if (this.FieldObjects[j].type == "appleEnemy" || this.FieldObjects[j].type == "BananaBunchEnemy" ||
				this.FieldObjects[j].type == "BananaEnemy" || this.FieldObjects[j].type == "CocoEnemy" ||
				this.FieldObjects[j].type == "vineEnemy" || this.FieldObjects[j].type == "EvilTree") 
                {
                    if (this.touchCheck(this.GreenMonsterObjects[i], j)) 
                    {
                        this.FieldObjects[j].hp -= this.GreenMonsterObjects[i].hp;
                        this.FieldObjects[j].UpdateHP();
                        if (this.GreenMonsterObjects[i].type != "Laser")
                        {
                            if (this.GreenMonsterObjects[i].type == "shrapnel")
                            {
                                var shrapnelArray =
                                [
                                new GreenMonsterBullet({ width: 8, height: 8, speedT: 5, hp: 5, top: this.FieldObjects[j].top, left: this.FieldObjects[j].left + this.FieldObjects[j].width / 2, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedB: 5, hp: 5, top: this.FieldObjects[j].top + this.FieldObjects[j].height, left: this.FieldObjects[j].left + this.FieldObjects[j].width / 2, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedR: 5, hp: 5, top: this.FieldObjects[j].top + this.FieldObjects[j].height / 2, left: this.FieldObjects[j].left + this.FieldObjects[j].width, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedL: 5, hp: 5, top: this.FieldObjects[j].top + this.FieldObjects[j].height / 2, left: this.FieldObjects[j].left, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedT: 5, speedL: 5, hp: 5, top: this.FieldObjects[j].top, left: this.FieldObjects[j].left, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedB: 5, speedR: 5, hp: 5, top: this.FieldObjects[j].top + this.FieldObjects[j].height, left: this.FieldObjects[j].left + this.FieldObjects[j].width, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedT: 5, speedR: 5, hp: 5, top: this.FieldObjects[j].top, left: this.FieldObjects[j].left + this.FieldObjects[j].width, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i]),
                                new GreenMonsterBullet({ width: 8, height: 8, speedB: 5, speedL: 5, hp: 5, top: this.FieldObjects[j].top + this.FieldObjects[j].height, left: this.FieldObjects[j].left, type: "ShrapnelFragment" }, this.GreenMonsterObjects[i])
                                ];
                                this.GreenMonsterObjects[i].destroy();
                                this.GreenMonsterObjects[i] = this.GreenMonsterObjects[this.GreenMonsterObjects.length - 1];
                                this.GreenMonsterObjects.pop();
                                endcheck = true;
                                for (var k = 0; k < shrapnelArray.length; k++)
                                    this.AddGreenMonsterObject(shrapnelArray[k]);
                            }
                            else
                            {
                                this.GreenMonsterObjects[i].destroy();
                                this.GreenMonsterObjects[i] = this.GreenMonsterObjects[this.GreenMonsterObjects.length - 1];
                                this.GreenMonsterObjects.pop();
                                endcheck = true;
                            }
                        }
                    }
                }
            }
            if (!endcheck)
                if (this.GreenMonsterObjects[i].left + this.GreenMonsterObjects[i].width > 800 || this.GreenMonsterObjects[i].left < 0
			    || this.GreenMonsterObjects[i].top < 0 || this.GreenMonsterObjects[i].top + this.GreenMonsterObjects[i].height > 600)
                {
                    this.GreenMonsterObjects[i].destroy();
                    this.GreenMonsterObjects[i] = this.GreenMonsterObjects[this.GreenMonsterObjects.length - 1];
                    this.GreenMonsterObjects.pop();
                    endcheck = true;
					i--;
                }
       }
    }
})